package edu.nf.project.webs.employeeAndCustomerInformation;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.nf.project.entitys.dto.employeeAndCustomerInformation.CouponDTO;
import edu.nf.project.entitys.entity.employeeAndCustomerInformation.convention.Coupon;
import edu.nf.project.services.exception.derivedAnomalies.CouponException;
import edu.nf.project.services.employeeAndCustomerInformation.CouponService;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券管理控制器
 */
@RestController
@RequestMapping("/api/coupons")
public class CouponController extends BaseController {
    @Autowired
    private CouponService couponService;

    /**
     * 转换器的预处理
     * 注册预定义转换器
     * 使用@InitBinder注解标注的方法会在执行当前Controller类
     * 的任意方法前执行，通过传入的WebDateBinder参数
     * 来设置日期的格式化，这样springmvc会根据指定的日期格式来转换
     *
     * @param binder
     */
    @InitBinder
    public void regFormatter(WebDataBinder binder){
        binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }

    /**
     * 获取优惠券列表
     */
    @GetMapping
    public ResultVO<Page<Coupon>> getCouponList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<Coupon> page = new Page<>(pageNum, pageSize);
            Page<Coupon> result = couponService.getCouponList(page);
            return success(result);
        } catch (Exception e) {
            if (e instanceof CouponException) {
                throw e;
            }
            throw new CouponException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 优惠卷列表展示
     * @return
     */
    @GetMapping("/couponInfo")
    public ResultVO<List<Coupon>> listCouponInfo(){
        try{
            List<Coupon> coupons = couponService.listCouponInfo();
            return success(coupons);
        }catch (Exception e) {
            if (e instanceof CouponException) {
                throw e;
            }
            throw new CouponException(40000, "系统错误，请稍后重试");
        }
    }

    @GetMapping("/coupon-user/{uId}")
    public ResultVO<List<CouponDTO>> listCouponByUsersId(@PathVariable Integer uId){
        try {
            List<CouponDTO> couponDTOS = couponService.listCouponByUsersId(uId);
            return success(couponDTOS);
        } catch (Exception e) {
            if (e instanceof CouponException) {
                throw e;
            }
            throw new CouponException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取优惠券详细信息
     */
    @GetMapping("/{couponId}")
    public ResultVO<Coupon> getCouponInfo(@PathVariable Integer couponId) {
        try {
            Coupon coupon = couponService.getCouponInfo(couponId);
            if (coupon == null) {
                throw new CouponException(40001, "优惠券不存在");
            }
            return success(coupon);
        } catch (Exception e) {
            if (e instanceof CouponException) {
                throw e;
            }
            throw new CouponException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 创建新优惠券
     */
    @PostMapping
    public ResultVO<Boolean> createCoupon(@RequestBody Coupon coupon) {
        try {
            boolean result = couponService.createCoupon(coupon);
            return success(result);
        } catch (Exception e) {
            if (e instanceof CouponException) {
                throw e;
            }
            throw new CouponException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 将优惠券分配给顾客
     */
    @PutMapping("/customers/{customerId}")
    public ResultVO<Boolean> assignCouponToCustomer(
            @PathVariable Integer customerId,
            @RequestParam Integer couponId) {
        try {
            boolean result = couponService.assignCouponToCustomer(customerId, couponId);
            return success(result);
        } catch (Exception e) {
            if (e instanceof CouponException) {
                throw e;
            }
            throw new CouponException(40000, "系统错误，请稍后重试");
        }
    }

    /**
     * 将优惠券分配给会员
     */
    @PutMapping("/members/{memberId}")
    public ResultVO<Boolean> assignCouponToMember(
            @PathVariable Integer memberId,
            @RequestParam Integer couponId) {
        try {
            boolean result = couponService.assignCouponToMember(memberId, couponId);
            return success(result);
        } catch (Exception e) {
            if (e instanceof CouponException) {
                throw e;
            }
            throw new CouponException(40000, "系统错误，请稍后重试");
        }
    }

    @PostMapping("/add")
    public ResultVO<Boolean> addCouponCustomerInfo(Integer customer_id, Integer coupon_id, Long deadline_time){
        try{
            if (deadline_time == null) {
                deadline_time = 7L; // 默认 7 天
            }
            System.out.println(customer_id);
            return success(couponService.addCouponCustomer(customer_id, coupon_id, deadline_time));
        }catch (Exception e) {
            if (e instanceof CouponException) {
                throw e;
            }
            System.out.println(e);
            throw new CouponException(40000,"系统错误，请稍后重试");
        }
    }

    @PostMapping("/update")
    public ResultVO<Boolean> updCoupon(@RequestBody Coupon coupon){
        try{
            Boolean flag = couponService.updCoupon(coupon);
            return success(flag);
        }catch (Exception e) {
            if (e instanceof CouponException) {
                throw e;
            }
            throw new CouponException(40000, "系统错误，请稍后重试");
        }
    }
} 